package com.zrgg.futuwen.network;


import com.zrgg.futuwen.utils.Logger;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.framed.Header;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Okhttp 工厂
 * Created by LL on 16/3/8.
 */
public class OkHttpFactory {

    static private long timeout_connect = 30L;

    static private long timeout_read = 30L;

    /**
     * 为了方便增加拦截机 普通的okhttp
     *
     * @return
     */
    public static OkHttpClient.Builder creatOkhttp() {
        OkHttpClient.Builder client = new OkHttpClient.Builder()
                .connectTimeout(timeout_connect, TimeUnit.SECONDS)
                .readTimeout(timeout_read, TimeUnit.SECONDS)
                .addInterceptor(new HeaderInterceptor());
//        client.addInterceptor(new VendorInterceptor());
//        client.addInterceptor(new AuthorizeInterceptor());

        if (Logger.isDebug) {
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                public void log(String message) {
                    Logger.v("OkHttpClient", message);
                }
            });
            // set your desired log level
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            client.addInterceptor(logging);
        }

        return client;
    }

    public static class HeaderInterceptor implements Interceptor {
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request()
                    .newBuilder()
                    .addHeader("User-Agent", "futuwen_ftw_android")
                    .build();
            return chain.proceed(request);
        }
    }
}
